<?php

namespace App\Http\Controllers\home;

// use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use Request;

class ShopcartController extends BaseController
{
    //购物车列表
    public function index()
    {
        $this -> go_login();
        $user_name = Cookie::get('u_name');
        $user = DB::table('user') -> select('user_id') -> where("user_name",$user_name) -> first();
        $user_id = $user -> user_id;
        
        // DB::connection()->enableQueryLog();
        $shopcart = DB::table('shopcar as car')
                    -> leftJoin("good as g","g.good_id","car.good_id")
                    -> leftJoin("goodcolor as gc","gc.goodcolor_id","car.goodcolor_id")
                    -> leftJoin("goodsize as gz","gz.goodsize_id","car.goodsize_id")
                    -> where("user_id",$user_id) 
                    -> get();
        // dump(DB::getQueryLOg());die;
        // dd($shopcart);
        $total = 0;//订单总价
        foreach($shopcart as $k => $v){
            $good_price = $v -> good_price;
            $number = $v -> number;
            $good_price1 = $good_price * $number;
            $shopcart[$k]->good_price1 = $good_price1;

            $total += $good_price1;
        }
        return view('home/cart',compact("shopcart","total"));
    }

    //添加购物车
    public function add()
    {
        $user_name = Cookie::get('user_name');
        $user = DB::table('user')-> select('user_id') -> where("user_name",$user_name) -> first();
        $user_id = $user["user_id"];

        $input = Request::all();
        if(!empty($input['wish_id'])){//从心愿单添加到购物车
            $wish_id = $input['wish_id'];
            $wish = DB::table('wish') -> where("wish_id",$wish_id) -> first();
            $data = array(
                'good_id' => $wish->good_id,
                'user_id' => $user_id,
                'goodcolor_id' => $wish->goodcolor_id,
                'goodsize_id' => $wish->goodsize_id,
                'number' => '1',
                'shopcar_addtime' => time(),
            );
            $res = DB::table("shopcar") -> insert($data);//添加购物车
            // DB::table("wish") -> where("wish_id",$wish_id) -> delete();//删除心愿单
            if($res){
                return redirect("/shopcart");
            }else{
                return redirect("/wihslist");
            }
        }
        // else{//商品详情页添加到购物车
        //     $good_id = $input['good_id'];
        //     $cart = DB::table("shopcar") -> whereRaw("user_id = $user_id AND good_id = $good_id") -> first();
        //     if($cart){
        //         $number = $cart->number;
        //         $data = array(
        //             'good_id' => $good_id,
        //             'user_id' => $user_id,
        //             'goodcolor_id' => $input['goodcolor_id'],
        //             'goodsize_id' => $input['goodsize_id'],
        //             'number' => $input['number'] + $number,
        //             'shopcar_addtime' => time(),
        //         );
        //         $res = DB::table("shopcar") -> insert($data);
        //         if($res){
        //             return redirect("/shopcart");
        //         }else{
        //             return redirect("/s_product/$good_id");
        //         }
        //     }
            
        // }
        
    }

    //删除购物车
    public function delete($cid)
    {
        // DB::connection()->enableQueryLog();
        $res = DB::table("shopcar") -> where("id",$cid) -> delete();
        // dump(DB::getQueryLog());
        if($res){
            return redirect('/shopcart');
        }else{
            return redirect('/shopcart');
        }
    }
}
